package com.atwork.wuhua.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.TypeSelectRegionAdapter;
import com.atwork.wuhua.bean.TypeSelectRegionBean;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

public class PopRegion extends PopupWindow {
    private View conentView;
    private Context context;
    private RecyclerView rvRegion;
    private List<TypeSelectRegionBean.DataBean> typeSelectRegionBeans;
    private TypeSelectRegionAdapter typeSelectRegionAdapter;
    private OnSelectLinstener onSelectLinstener;

    public interface OnSelectLinstener {
        void onselect(int  positon);
    }

    public PopRegion(Context context, List<TypeSelectRegionBean.DataBean> typeSelectRegionBeans,OnSelectLinstener onSelectLinstener) {
        this.context = context;
        conentView = LayoutInflater.from(context).inflate(R.layout.pop_region_select, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //让pop可以点击外面消失掉
        this.setBackgroundDrawable(new ColorDrawable(0));
//        // 刷新状态
//        this.update();
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.dialogStyle_top);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        this.onSelectLinstener=onSelectLinstener;
        this.typeSelectRegionBeans = typeSelectRegionBeans;
        initView();
    }

    private void initView() {
        rvRegion = conentView.findViewById(R.id.rv_region);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvRegion.setLayoutManager(manager);

        typeSelectRegionAdapter = new TypeSelectRegionAdapter(context,R.layout.item_type_region_select, typeSelectRegionBeans);
        rvRegion.setAdapter(typeSelectRegionAdapter);

        typeSelectRegionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.rly_click:
                        for (int i = 0; i < typeSelectRegionBeans.size(); i++) {
                            if (i == position) {
                                typeSelectRegionBeans.get(i).setSelect(true);
                                onSelectLinstener.onselect(position);
                            } else {
                                typeSelectRegionBeans.get(i).setSelect(false);
                            }
                        }
                        typeSelectRegionAdapter.setNewData(typeSelectRegionBeans);
                        dismiss();
                        break;
                }
            }
        });
    }
}
